package com.spring.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.spring.gowhere.R;
import com.spring.httputils.RequestParams;
import com.spring.httputils.RestClient;
import com.spring.httputils.TextHttpResponseHandler;
import com.spring.model.InfoData;
import com.spring.model.ReturnInfoList;
import com.spring.tool.App;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的消息
 * @author Administrator
 *
 */
public class MyMessageActivity extends BaseActivity{
	RestClient restClient = null;
	String username = null;
	String logintype = null;
	Button btnupmore,btndownmore;
	private int index=1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginactivity);
		SharedPreferences userInfo = getSharedPreferences(
				"user_info", 0);
		username = userInfo.getString("username", "");
		logintype = userInfo.getString("logintype", "");
		btnupmore = (Button) findViewById(R.id.btnupmore);
		btndownmore =(Button)findViewById(R.id.btndownmore);
		restClient = new RestClient(MyMessageActivity.this);
		SendHttpData sendhttpdata = new SendHttpData();
		sendhttpdata.run();
		
		btnupmore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(index>1){					
					index--;
				}
				SendHttpData sendhttpdata = new SendHttpData();
				sendhttpdata.run();
			}
		});
		
		btndownmore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				index++;
				// TODO Auto-generated method stub
				SendHttpData sendhttpdata = new SendHttpData();
				sendhttpdata.run();
			}
		});
	}
	class SendHttpData implements Runnable {

		@Override
		public void run() {
			// String url =
			// "http://120.237.110.50:8097/andriodapp/WebService.asmx?op=EmployeeLogin";
			String url = App.getInstance().url+"/QueryMyInfoList";
			RequestParams requestparams = new RequestParams(url, "POST");
			requestparams.put("LoginAccount", username);
			requestparams.put("LoginType", logintype);
			requestparams.put("PageIndex", index);
			TextHttpResponseHandler textHttpResponseHandler = new TextHttpResponseHandler() {

				@Override
				public void onSuccess(int statusCode, Header[] headers,
						String responseString) {
					// TODO Auto-generated method stub
					Log.e("TAG", "responseString---" + responseString);
					AnalysisData(responseString);

				}

				@Override
				public void onFailure(int statusCode, Header[] headers,
						String responseString, Throwable throwable) {
					// TODO Auto-generated method stub
					Log.e("TAG", "responseString---" + responseString);
					AnalysisData(responseString);

				}
			};
			try {
				restClient.post(url, requestparams, textHttpResponseHandler);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		/**
		 * json数据解析
		 * 
		 * @param strResult
		 * @throws JSONException
		 */
		private void AnalysisData(String strResult) {
			/**
			 * 这里需要分析服务器回传的json格式数据，
			 */
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject jsonObject = null;
			try {
				if (jsonParser != null) {
					
					jsonObject = (JSONObject) jsonParser.nextValue();
				}
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				JSONArray jsonArray = jsonObject.getJSONArray("InfoList");
				ReturnInfoList message = new ReturnInfoList();
				List<InfoData> lists = new ArrayList<InfoData>();
				for(int i =0;i <jsonArray.length();i++){
					 JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i);
					 InfoData infodata = new InfoData();
					 infodata.setInfoCaption(jsonObject2.getString("InfoCaption"));
					 infodata.setInfoContent(jsonObject2.getString("InfoContent"));
					 infodata.setInfoSource(jsonObject2.getString("InfoSource"));
					 infodata.setInfoDate(jsonObject2.getString("InfoDate"));
					 infodata.setInfoFlag(jsonObject2.getString("InfoFlag"));
					 infodata.setReservedField1(jsonObject2.getString("ReservedField1"));
					 infodata.setReservedField2(jsonObject2.getString("ReservedField2"));
					 lists.add(infodata);
				}
				if(lists!=null&&lists.size()>0){
					message.setInfoList(lists);
					 message.setReturnMsg(jsonObject.getString("returnMsg"));
				}else{
					Toast.makeText(MyMessageActivity.this, "没有查询到相关数据",  Toast.LENGTH_SHORT).show();
				}
				 

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
